package com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketTemplateMapper;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicketTemplate;
import com.xidian.zhiancloud.qo.dangerWorkTicket.TemplateListQO;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketTemplateQO;
import com.xidian.zhiancloud.ticketTemplate.ticketContentRisk.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;


@Service
public class WorkTicketTemplateService extends ServiceImpl<WorkTicketTemplateMapper, WorkTicketTemplate> implements IService<WorkTicketTemplate> {

    @Autowired
    private WorkTicketTemplateMapper workTicketTemplateMapper;

    public Boolean templateInit(Long companyId) {
        //注入所有的template
        WorkTicketTemplate fire = new WorkTicketTemplate();
        fire.setContent(Fire.fireRiskStr());
        fire.setName("动火作业票");
        fire.setCompanyId(companyId);
        fire.setType(1);
        save(fire);

        WorkTicketTemplate restrictedSpace = new WorkTicketTemplate();
        restrictedSpace.setContent(RestrictedSpace.restrictedRiskStr());
        restrictedSpace.setName("受限空间作业票");
        restrictedSpace.setCompanyId(companyId);
        restrictedSpace.setType(2);
        save(restrictedSpace);

        WorkTicketTemplate highSpace = new WorkTicketTemplate();
        highSpace.setContent(HighSpace.HighSpaceRiskStr());
        highSpace.setName("高处作业票");
        highSpace.setCompanyId(companyId);
        highSpace.setType(3);
        save(highSpace);

        WorkTicketTemplate hoist = new WorkTicketTemplate();
        hoist.setContent(Hoist.hoistRiskStr());
        hoist.setName("吊装作业票");
        hoist.setCompanyId(companyId);
        hoist.setType(4);
        save(hoist);

        WorkTicketTemplate turnoff = new WorkTicketTemplate();
        turnoff.setContent(Turnoff.turnoffRiskStr());
        turnoff.setName("断路作业票");
        turnoff.setCompanyId(companyId);
        turnoff.setType(5);
        save(turnoff);

        WorkTicketTemplate groundBreaking = new WorkTicketTemplate();
        groundBreaking.setContent(GroundBreaking.groundBreakingRiskStr());
        groundBreaking.setName("动土作业票");
        groundBreaking.setCompanyId(companyId);
        groundBreaking.setType(6);
        save(groundBreaking);

        WorkTicketTemplate temporaryPower = new WorkTicketTemplate();
        temporaryPower.setContent(TemporaryPower.temporaryPowerRiskStr());
        temporaryPower.setName("临时用电作业票");
        temporaryPower.setCompanyId(companyId);
        temporaryPower.setType(7);
        save(temporaryPower);

        WorkTicketTemplate blindBoardBlockUp = new WorkTicketTemplate();
        blindBoardBlockUp.setContent(BlindBoardBlockUp.BlindBoardBlockUpRiskStr());
        blindBoardBlockUp.setName("盲板抽堵作业票");
        blindBoardBlockUp.setCompanyId(companyId);
        blindBoardBlockUp.setType(8);
        save(blindBoardBlockUp);

        WorkTicketTemplate raysHarm = new WorkTicketTemplate();
        raysHarm.setContent(RaysHarm.raysHarmRiskStr());
        raysHarm.setName("射线探伤安全作业票");
        raysHarm.setCompanyId(companyId);
        raysHarm.setType(9);
        save(raysHarm);

        return Boolean.TRUE;
    }


    /**
     * 根据companyId和status查表返回状态为开启的作业票模板
     *
     * @param templateListQO
     * @return
     */
    public LinkedHashMap<Long, String> getListByQO(TemplateListQO templateListQO) {

        List<WorkTicketTemplate> templateList = workTicketTemplateMapper.getList(templateListQO);
        LinkedHashMap<Long, String> templateListMap = new LinkedHashMap<>();
        // 遍历List元素
        for (int i = 0; i < templateList.size(); i++) {
            WorkTicketTemplate template = templateList.get(i);
            templateListMap.put(template.getId(), template.getName());
        }
        return templateListMap;
    }

    /**
     * 返回作业票模板列表
     *
     * @param workTicketTemplateQO
     * @return 作业票模板
     */
    public IPage<WorkTicketTemplate> getList(WorkTicketTemplateQO workTicketTemplateQO) {
        Page<WorkTicketTemplate> page = new Page<>(workTicketTemplateQO.getCurrent(), workTicketTemplateQO.getSize());
        IPage<WorkTicketTemplate> res = workTicketTemplateMapper.getTemplateList(page, workTicketTemplateQO);
        return res;
    }

    /**
     * 根据作业票模板id返回RiskList
     *
     * @param templateId
     * @return
     */
    public List<String> riskList(Long templateId) {
        String risk_s =  getById(templateId).getContent();                                                   // 风险辨识
        List<String> risks = new LinkedList<>();
        for(String risk : risk_s.split("。")) {
            risks.add(risk);
        }
        return risks;
    }

    public List<String> riskList(String riskStr) {
        List<String> risks = new LinkedList<>();
        for(String risk : riskStr.split("。")) {
            risks.add(risk);
        }
        return risks;
    }
}
